Ccay.Module.define("Ccay.example.ui.igrid.editorFunction",function(page,$S){
	page.ready = function(){
		
		var gridParam1 = {
			editPermission: "enumManagement:update",
			editMode: true,
			showPager: false,
		    columns: [
		 		{field: 'name', width:100, header: "string"},
		 		{field: 'sex', width:70, header: "bool", type: 'bool'},
		 		{field: 'age', width:90, header: "integer", type: 'integer', rule: {range: [16, 80]}},
		 		{field: 'tuition', width:100, header: "currency", type: 'currency', format:{fraction:2, flag:'￥[currency]'}, rule: {range: [-9999999999, 9999999999]}},
		 		{field: 'birthday', width:100, header: "date", type: 'date'},
		 		{field: 'province', width:100, header: "lookup", type: 'lookup', format: {supportLang: true, code: "province"}},
		 		{field: 'blog', width:140, header: "textarea", type: 'textarea'}
		    ],
		    fnEndRowEdit: function (rowData, row) {
		    	Ccay.Core.log("2");
		    },
			ajax:{
			   	url: 'services/ccay/demo/student/list/page/5/1/${orderBy}'
			}
		};
		
		var gridParam2 = {
			editMode: "insert",
			showPager: false,
		    columns: [
		 		{field: 'name', width:70, header: "string"},
		 		{field: 'sex', width:60, header: "bool", type: 'bool'},
		 		{field: 'age', width:70, header: "integer", type: 'integer', rule: {range: [16, 80]}},
		 		{field: 'tuition', width:80, header: "currency", type: 'currency', format:{fraction:2, flag:'￥[currency]'}, rule: {range: [-9999999999, 9999999999]}},
		 		{field: 'birthday', width:100, header: "date", type: 'date'},
		 		{field: 'province', width:100, header: "lookup", type: 'lookup', format: {supportLang: true, code: "province"}},
		 		{field: 'blog', width:140, header: "textarea", type: 'textarea'}
		    ],
			ajax:{
			   	url: 'services/ccay/demo/student/list/page/5/1/${orderBy}'
			},
			operation:{
			    insert:{
		    		btn:'#btnAdd',
		    		defaultRow:{sex: 1, blog:'http://'}
		    	}
			}
		};
		
		var gridParam3 = {
			editMode: true,
			rowEdit: true,
			showPager: false,
		    columns: [
		 		{field: 'name', width:100, header: "string"},
		 		{field: 'sex', width:70, header: "bool", type: 'bool'},
		 		{field: 'age', width:90, header: "integer", type: 'integer', rule: {range: [16, 80]}},
		 		{field: 'tuition', width:100, header: "currency", type: 'currency', format:{fraction:2, flag:'￥[currency]'}, rule: {range: [-9999999999, 9999999999]}},
		 		{field: 'birthday', width:100, header: "date", type: 'date'},
		 		{field: 'province', width:100, header: "lookup", type: 'lookup', format: {supportLang: true, code: "province"}},
		 		{field: 'blog', width:140, header: "textarea", type: 'textarea'}
		    ],
			ajax:{
			   	url: 'services/ccay/demo/student/list/page/5/1/${orderBy}'
			}
		};
		
		// 第一级联想框					
		var connectorOp = {
			valueField:"i18nKey",
			displayField:'i18nKey',
			fieldMapper: {
		       i18nKey: $i18n("ccay.i18n.label.key")
		    },
			queryParam:function(){
			  return {"i18nKey":$('#suggest_trigger').val()}
			},
			url:"services/manage/i18n/list/page/80/1"
		};

		// 第二级联想框	    
		var connectorOp2 = {
			valueField:"i18nKey",
			displayField:'i18nContent',
			lazyload: false,
			fieldMapper: {
		       i18nContent: $i18n("ccay.i18n.label.content")
		    },
			ajax:{
		   	 	url:"services/manage/i18n/list/page/80/1?${connect}"
		    },
		    connector:[
		 				{target:"#suggestProvince",paramName:"i18nKey",defaultValue:""}
			]
		};
		var gridParam4 = {
			height: 130,
			editMode: true,
			showPager: false,
		    columns: [
		 		{field: 'name', width:100, header: "sName"},
		 		{field: 'province', width:150, header: "sProvince(combox)", type: 'enum', editorOp: {id: "comboProvince", afterTrigger: ["address"]}, format: {service:"REST:services/ccay/demo/address/listAllProvinces",valueField: "provinceId", displayField: "provinceName"}},
		 		{field: 'address', valueField: 'city', width:150, header: "sCity", type: 'enum', editorOp: {trigger: ["province", "name"], conf: {connector: "#comboProvince", lazyload: false, ajax: {url:'services/ccay/demo/address/listCitiesByProvincesId/{0}'}}}, format: {valueField: "cityId", displayField: "cityName"}},
		 		{field: 'attr3', width:150, header: "i18nKey(suggest)", type: 'suggest', editorOp: {id: "suggestProvince", afterTrigger: ["attr4"], conf: connectorOp}},
		 		{field: 'attr4', width:150, header: "i18nContent", type: 'suggest', editorOp: { conf: connectorOp2,trigger: ["attr3"]}}	 	
		 		],
			ajax:{
			   	url: 'services/ccay/demo/student/list/page/5/1/${orderBy}'
			}
		};
		
		var suggestOps = {
			multi: true,
			url: "services/ccay/demo/student/list/page/${suggestNum}/1",
			valueField: "id",
			displayField: "name",
			fieldMapper: {
				name: "sName",
				sex: "sSex",
				address: "sAddress"
			}
		};
		
		var popupOps = {
			igrid: true,
			multiSelect: true,
			servicePath: "services/ccay/demo/student/list",
			uniqueKey: "id",
			displayField: "name",
			columns: [
				{header: "sName", field: 'name'},
				{header: "sSex", field: 'sex'},
				{header: "sAddress", field: 'address'}
			]
		};
		
		//----------
		var columns1 = [ {
			header : $i18n("ccay.security.label.roleName"),
			id : 'name',
			isCondition : true
			}, {
			header : $i18n("ccay.common.label.description"),
			id : 'id',
			isCondition : true
			} ];
		
		var Usert1 = {
				suggestOp : {
				    url : "services/ccay/demo/student/list/page/${suggestNum}/1",
					fieldMapper : {
						age : "Age",
						name : $i18n("ccay.common.label.description")
					}
				},
				popOp : {
					  servicePath: "services/ccay/demo/student/list",
					uniqueKey : "id",
					columns : columns1,
					title : $i18n("ccay.registry.label.popupTitle")
				},
				 displayField : "name",
				 valueField : "id"
			}
		//-------
		
		
		var gridParam5 = {
			height: 330,
			wrap: true,
			editMode: true,
			showPager: false,
		    columns: [   
		 		{field: 'name', width:100, header: "text(default)"},
		 		{field: 'attr3', width:110, header: "ldap", type: 'ldap', editorOp: {conf: {multi: true}}},
		 		{field: 'name', valueField: "id", width:100, header: "suggest", renderer: 'enum', editorOp: {type: "suggest", conf: suggestOps}},
		 		{field: 'attr4', width:110, header: "hrdept", renderer: 'enum', editorOp: {type: 'hrdept'}},
		 		{field: 'attr5', width:110, header: "hrapprover", renderer: 'enum', editorOp: {type: 'hrapprover'}},
		 		{field: 'mail', width:110, header: "popup", renderer: 'enumExtend', editorOp: {type: "popup", conf: popupOps}},
		 		{field : 'phone',width : 150,header : "popbox",type:"popbox",editorOp:{conf:Usert1}},
		 		{field: 'sex', width:70, header: "bool", type: 'bool'},
		 		{field: 'birthdayEnd', width:90, header: "dateym", type: 'dateym'},
		 		{field: 'birthday', width:90, header: "date", type: 'date'},
		 		{field: 'lastUpdateDate', width:120, header: "datetime", type: 'datetime',content: "${#getLastTime(lastUpdateDate)}"},
		 		{field: 'province', width:100, header: "lookup", type: 'lookup', format: {supportLang: true, code: "province"}, rule: {required: true}},
		 		{field: 'card', width:140, header: "combobox/enum", type: 'enum', editorOp: {conf:{multi: true}}, format: {service: 'FN:Ccay.example.ui.igrid.editorFunction.provinceData', cacheKey: "render_provinceData", valueField: "pCode", displayField: "pName"}},
		 		{field: 'blog', width:135, header: "textarea", renderer: "longtext", format: {height: "50px"}, editorOp:{type: 'textarea', width:"50px", height:"50px"}},
		 		/** 
		 		 * 国家，只需要数据库中一个字段来存储
		 		 **/
	 		 	{field: "country",width:110, header: "country", type: 'country'},
		 		
		 		/**	
		 		 * 公司，需要对应数据库中两个字段
		 		 * 需要设定valueField：保存value的字段；field：用于显示的字段
		 		 * */
		 		 {field: "companyName",valueField:"companyCode",width:110, header: "company", type: 'company'},
		 		/**
		 		 * 货币，只需要数据库中一个字段来存储
		 		 * */
		 		 {field: "currency",width:110, header: "Currency", type: 'iCurrency'},
		 		/**	
		 		 * 片区代办，需要对应数据库中两个字段
		 		 * 需要设定valueField：保存value的字段；field：用于显示的字段
		 		 * 设置format中的areaLabel为按不同类型划分片区,值可以是"JCR""Region""Office""Rep. Office"
		 		 * */
		 		 {field: "areaName",valueField:"areaCode",width:110, header: "area", type: 'area',format:{areaLabel:"Region"}},
		 		/**
		 		 * 行政区划分，只需要数据库中一个字段来存储locationName，
		 		 * 此处包含级联查询，level：级联等级number，1级联等级最高，2其次，类推。connector：级联字段
		 		 * 此处需要做联动，防止查询错误。
		 		 * */
		 		 {field: 'location',width:110, header: "location", type: 'location',format:{level:1},editorOp: {afterTrigger: ["attr1", "attr2"]}},
		 		 {field: 'attr1',width:110, header: "location(1)", type: 'location',format:{level:2,connector:"#location"},editorOp: {trigger: ["location"],afterTrigger: ["attr2"]}},
		 		 {field: 'attr2',width:110, header: "location(2)", type: 'location',format:{level:3,connector:"#attr1"},editorOp: {trigger: ["location", "attr1"]}},
		 		/**
		 		 * PBI,数据库中一个字段用来保存整个对象
		 		 * readonly:boolean,是否只读
		 		 */
		 		{field: "pbi",width:110, header: "pbi", type: 'pbi',format:{readonly:false}},
		 		{field: 'money', width:110, header: "money", type: 'money', editorOp: {currencyCodeName:"currencycode",exchangeDateName:"exchangedate",amountName:"amount"
				}}
		    ],
			ajax:{
			   	url: 'services/ccay/demo/student/list/page/15/1/${orderBy}'
			}
		};
		
		var editorOps = {
			build: function(container, op){
    			var ctr = $.create("SELECT","ccay-editor-select").appendTo(container);
    			var service = op.data;
    			if($.isArray(service)){
    				for(var i=0; i<service.length; i++){
    					ctr.append("<option value='{0}'>{1}</option>".format(service[i].value, service[i].text));
    				}
    			}
    			return ctr;
    		},
    		getValue: function (ctr) {
		    	var valueObj = {
		    		value: ctr.val() || "",
		    		text: ctr.find("option:selected").text() || ""
		    	};
		    	return valueObj;
		    },
		    setValue: function (ctr, valueObj) {
		        ctr.val(valueObj.value);
		    }
		};
		
		var gridParam6 = {
			height: 130,
			editMode: true,
			showPager: false,
			allowSort: false,
		    columns: [
		    	{field: 'grade', width:120, header: "sGrade(normal)", type: 'enum', format: {data: Ccay.example.ui.igrid.editorFunction.classData}},
		 		{field: 'classes', width:120, header: "sClass(editor)", type: 'enum', format: {data: Ccay.example.ui.igrid.editorFunction.classData}, editorOp: {data: Ccay.example.ui.igrid.editorFunction.classData}, editor: Ccay.Editor.create(editorOps)}
		    ],
			ajax:{
			   	url: 'services/ccay/demo/student/list/page/10/1/${orderBy}'
			}
		};
		
		$S("#editorFunctionDemo").ccayTab();
		
		getLastTime = function(data){
			return Ccay.Format.toDateBefore(data && data.getUTCTime());
		};
		
		//点击tab头后，才加载数据，只第一次生效
		$S("#hd1").one("click", function () {
			$S("#iStudent1").igrid(gridParam1);
		});
		
		$S("#hd2").one("click", function () {
			$S("#iStudent2").igrid(gridParam2);
		});
		
		$S("#hd3").one("click", function () {
			$S("#iStudent3").igrid(gridParam3);
		});
		
		$S("#hd4").one("click", function () {
			$S("#iStudent4").igrid(gridParam4);
		});
		
		$S("li[tabid='tab3']").one("click", function () {
			$S("#iStudent5").igrid(gridParam5);
		});
		
		$S("li[tabid='tab4']").one("click", function () {
			$S("#iStudent6").igrid(gridParam6);
		});
	};
	
	page.provinceData = function(){
		var provinceList = [
			{"pCode": 0, "pName": "广东省"},
			{"pCode": 1, "pName": "广西省"},
			{"pCode": 2, "pName": "湖北省"},
			{"pCode": 3, "pName": "湖南省"},
			{"pCode": 4, "pName": "四川省"},
			{"pCode": 5, "pName": "山东省"},
			{"pCode": 6, "pName": "山西省"}
		];
		return provinceList;
	};
	
	page.classData = [
		{value: 1, text: "一班"},
		{value: 2, text: "二班"},
		{value: 3, text: "三班"},
		{value: 4, text: "四班"},
		{value: 5, text: "五班"},
		{value: 6, text: "六班"},
		{value: 7, text: "七班"},
		{value: 8, text: "八班"},
		{value: 9, text: "九班"},
		{value: 10, text: "十班"}
	];
	
	page.openDemo = function(tabId){
		$S("#editorFunctionDemo").getCcayTabManager().selectItem(tabId);
		$S("li[tabid='" + tabId + "']").click();
	};
	
});